<?php

namespace app\api\controller;

use addons\epay\library\Service;
use app\admin\model\goods\Order;
use app\common\controller\Api;
use app\common\controller\Payment;
use think\Controller;
use think\Db;
use think\Request;


class Wanlshopgoodscomment extends Api
{
    private static $model;
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';


    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        self::$model = new \app\common\model\WanlshopGoodsComment();
    }

    /**
     * 评论列表
     */
    public function lists()
    {
        $num = input('num');
        $pageSize = input('pagesize', 15);

        $goods_id = input('goods_id');
        $goodsInfo = \app\common\model\WanlshopGoods::find($goods_id);
        if (!$goodsInfo) {
            $this->error('该商品不存在！');
        }

        $where = [
            'goods_id' => $goods_id,
        ];

        $field = 'id,user_id,goods_id,content,images,score,createtime';
        if ($num > 0) {
            $list = self::$model->field($field)->where($where)->order('updatetime desc')->limit(0, $num)->select();
        } else {
            $list = self::$model->field($field)->where($where)->order('updatetime desc')->paginate($pageSize);
        }

        if ($list) {
            foreach ($list as $val) {
                $val->user;
            }
        }

        $this->success('商品评论', $list);
    }

    /**
     * 商品进行评论
     */
    public function create()
    {
        $goodsId = input('goods_id');
        if (!$goodsId) {
            $this->error('参数缺失！');
        }

        $goodsInfo = \app\common\model\WanlshopGoods::find($goodsId);
        if (!$goodsInfo) {
            $this->error('该商品不存在！');
        }
        $content = input('content');
        if (!$content) {
            $this->error('请输入评论内容！');
        }
        $userId = $this->auth->id;
        $data = [
            'user_id' => $userId,
            'shop_id' => $goodsInfo->shop_id,
            'goods_id' => $goodsId,
            'order_type' => 'score',
            'order_goods_id' => $goodsId,
            'content' => $content,
        ];

        $res = \app\common\model\WanlshopGoodsComment::create($data);
        if (!$res) {
            $this->error('评论失败！');
        }

        $this->success('评论成功！');
    }
}
